﻿"""
Copyright 2008 Olivier Belanger

This file is part of Ounk.

Ounk is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Ounk is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ounk.  If not, see <http://www.gnu.org/licenses/>.
"""

import wx, math

class Button(wx.Button):
    def __init__(self, parent, id, label, pos, function=None):
        wx.Button.__init__(self, parent, id, label, pos)
        self.function = function
        self.Bind(wx.EVT_BUTTON, self.call)
        
    def call(self, evt):
        self.function()
  
class Toggle(wx.ToggleButton):
    def __init__(self, parent, id, label, pos, function=None):
        wx.ToggleButton.__init__(self, parent, id, label, pos)
        self.function = function
        self.Bind(wx.EVT_TOGGLEBUTTON, self.call)
        
    def call(self, evt):
        self.function(evt.GetInt())
        
class SpinCtrl(wx.SpinCtrl):
    def __init__(self, parent, id, spinPos, size, min=0, max=100, initial=0, function=None):
        wx.SpinCtrl.__init__(self, parent, id, "", spinPos, size, min=0, max=100, initial=0,
                             style = wx.SP_ARROW_KEYS | wx.SP_WRAP)
        self.function = function
        self.SetValue(initial)
        self.Bind(wx.EVT_SPINCTRL, self.call)
        self.Bind(wx.EVT_TEXT, self.textCall)
        self.firstCall(initial)
        self.id = 0
        self.min = min
        self.max = max
        self.last = -9999999999999999
        
    def call(self, evt):
        print 'call'
        self.function(self.GetValue())        
        
    def firstCall(self, val):
        self.function(val)

    def checkRange(self, val):
        if val == self.last:
            self.SetValue(val)
            self.function(val)
        self.last = val
                
    def textCall(self, evt):
        if math.fabs(evt.GetId()) >= self.id:
            self.id = math.fabs(evt.GetId())
            self.checkRange(self.GetValue())
        else:
            pass
        evt.Skip()  

class Menu(wx.Choice):
    def __init__(self, parent, id, pos, choices, function=None):
        wx.Choice.__init__(self, parent, id, pos, size=wx.DefaultSize, choices=choices)
        self.function = function
        self.Bind(wx.EVT_CHOICE, self.call)
        
    def call(self, evt):
        self.function(evt.GetString())
